# 网络安全漏洞知识图谱 - Conda环境设置脚本

Write-Host "🚀 设置网络安全漏洞知识图谱项目的Conda环境" -ForegroundColor Blue
Write-Host ""

# 检查conda是否安装
Write-Host "1. 检查Conda环境..." -ForegroundColor Yellow
try {
    $condaVersion = conda --version 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "   ✅ Conda已安装: $condaVersion" -ForegroundColor Green
    } else {
        Write-Host "   ❌ Conda未安装或不在PATH中" -ForegroundColor Red
        Write-Host "   请先安装Miniconda或Anaconda" -ForegroundColor Red
        exit 1
    }
} catch {
    Write-Host "   ❌ 无法找到Conda命令" -ForegroundColor Red
    Write-Host "   请确保Conda已安装并添加到PATH中" -ForegroundColor Red
    exit 1
}

# 创建conda环境
Write-Host ""
Write-Host "2. 创建Conda环境..." -ForegroundColor Yellow
Write-Host "   环境名称: vulnerability-kg" -ForegroundColor Cyan
Write-Host "   这可能需要几分钟时间..." -ForegroundColor Cyan

try {
    conda env create -f environment.yml
    if ($LASTEXITCODE -eq 0) {
        Write-Host "   ✅ Conda环境创建成功" -ForegroundColor Green
    } else {
        Write-Host "   ⚠️ 环境可能已存在，尝试更新..." -ForegroundColor Yellow
        conda env update -f environment.yml --prune
        if ($LASTEXITCODE -eq 0) {
            Write-Host "   ✅ Conda环境更新成功" -ForegroundColor Green
        } else {
            Write-Host "   ❌ 环境创建/更新失败" -ForegroundColor Red
            Write-Host "   请检查environment.yml文件和网络连接" -ForegroundColor Red
            exit 1
        }
    }
} catch {
    Write-Host "   ❌ Conda环境创建过程中出错: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 激活环境
Write-Host ""
Write-Host "3. 激活Conda环境..." -ForegroundColor Yellow
Write-Host "   执行以下命令激活环境:" -ForegroundColor Cyan
Write-Host "   conda activate vulnerability-kg" -ForegroundColor White

# 验证安装
Write-Host ""
Write-Host "4. 验证关键依赖..." -ForegroundColor Yellow

# 创建验证脚本
$verifyScript = @"
import sys
import pkg_resources

# 关键依赖列表
critical_packages = [
    'fastapi', 'pytest', 'pandas', 'numpy', 'requests', 
    'faker', 'beautifulsoup4', 'sqlalchemy', 'pyyaml'
]

print("🔍 验证关键依赖安装...")
missing_packages = []
installed_packages = []

for package in critical_packages:
    try:
        version = pkg_resources.get_distribution(package).version
        installed_packages.append(f"{package}=={version}")
        print(f"   ✅ {package}: {version}")
    except pkg_resources.DistributionNotFound:
        missing_packages.append(package)
        print(f"   ❌ {package}: 未安装")

print(f"\n📊 验证结果:")
print(f"   ✅ 已安装: {len(installed_packages)}")
print(f"   ❌ 缺失: {len(missing_packages)}")

if missing_packages:
    print(f"\n⚠️ 缺失的包: {', '.join(missing_packages)}")
    print("请尝试重新运行环境设置或手动安装缺失的包")
    sys.exit(1)
else:
    print("\n🎉 所有关键依赖安装成功！")
    sys.exit(0)
"@

$verifyScript | Out-File -FilePath "verify_dependencies.py" -Encoding UTF8

# 运行验证（需要用户手动激活环境后执行）
Write-Host ""
Write-Host "5. 下一步操作:" -ForegroundColor Yellow
Write-Host "   请执行以下命令完成设置:" -ForegroundColor Cyan
Write-Host ""
Write-Host "   # 激活环境" -ForegroundColor White
Write-Host "   conda activate vulnerability-kg" -ForegroundColor Green
Write-Host ""
Write-Host "   # 验证依赖" -ForegroundColor White  
Write-Host "   python verify_dependencies.py" -ForegroundColor Green
Write-Host ""
Write-Host "   # 运行测试" -ForegroundColor White
Write-Host "   python tests/run_local_tests.py" -ForegroundColor Green
Write-Host ""

# 显示环境信息
Write-Host ""
Write-Host "📋 环境信息:" -ForegroundColor Blue
Write-Host "   环境名称: vulnerability-kg" -ForegroundColor White
Write-Host "   Python版本: 3.11" -ForegroundColor White
Write-Host "   主要组件: FastAPI, PyTorch, Scrapy, Pytest" -ForegroundColor White
Write-Host "   配置文件: environment.yml" -ForegroundColor White

Write-Host ""
Write-Host "🎯 常用命令:" -ForegroundColor Blue
Write-Host "   激活环境: conda activate vulnerability-kg" -ForegroundColor White
Write-Host "   停用环境: conda deactivate" -ForegroundColor White
Write-Host "   查看环境: conda env list" -ForegroundColor White
Write-Host "   更新环境: conda env update -f environment.yml --prune" -ForegroundColor White
Write-Host "   删除环境: conda env remove -n vulnerability-kg" -ForegroundColor White

Write-Host ""
Write-Host "✅ Conda环境设置脚本执行完成！" -ForegroundColor Green
Write-Host "请按照上述说明激活环境并验证安装。" -ForegroundColor Cyan 